import { defineStore } from 'pinia'
import qs from 'qs'
import jsCookie from 'js-cookie'
import { ElMessage } from "element-plus"
import router from '@/router'
import { post } from '@/utils/http'

export const useUserStore = defineStore({
    id: "userStore",
    // 开启持久化存储
    persist: {
        enabled: true
    },
    state: () => ({
        user: new UserInfo()        //用户信息
    }),
    actions: {
        // 设置用户信息
        setUser(user: UserInfo) {
            this.user = user
        },
        // 登出
        loginOut() {
            // let that = this;
            const data = {
                _gp: 'admin',
                _mt: 'logout'
            }
            post(data).then(res=>{
                
                let {errmsg,errno} = res.data
                if (200===errno) {
                    // user=null;             //清空状态存储                                      
                    // jsCookie.remove("token")    //删除cookie
                    router.replace("/login")
                } else {
                    router.replace("/login")
                    ElMessage.error(errmsg)
                }
            }).catch(err=>{
                console.log(err);
                
            })
        }
    }
})


class UserInfo {
    avatarUrl?: string;
    gmtCreate: number;
    gmtLastLogin: string;
    gmtUpdate: number;
    id: number;
    lastLoginIp: string;
    perms: string[];
    phone: string;
    realname: string;
    roleIds: number[];
    roles: string[];
    status: number;
    username: string;
    constructor() {
        this.avatarUrl = "";
        this.gmtCreate = 0;
        this.gmtLastLogin = "";
        this.gmtUpdate = 0;
        this.id = 0;
        this.lastLoginIp = "";
        this.perms = [];
        this.phone = "";
        this.realname = "";
        this.roleIds = [];
        this.roles = [];
        this.status = 0;
        this.username = "";
    }
}